﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_maintain.TMS_tyre
{
    public class TMS_tyres : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_tyres
    {
        public TMS_tyres(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }
        #region 油费显示
        public async Task<PageResponse<TMS_tyreDto>> GetTry(string TryNumber, string Trypin,string Trygui,string TryPeop, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TMS_tyreDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_tyreModel where 1=1 ";
            if (!string.IsNullOrEmpty(TryNumber))
            {
                sql += $" and TryNumber like '%{TryNumber}%'";
            }
            if (!string.IsNullOrEmpty(Trypin))
            {
                sql += $" and Trypin  like '%{Trypin}%'";
            }
            if (!string.IsNullOrEmpty(Trygui))
            {
                sql += $" and Trygui  like '%{Trygui}%'";
            }
            if (!string.IsNullOrEmpty(TryPeop))
            {
                sql += $" and TryPeop  like '%{TryPeop}%'";
            }

            var list = await dbConnection.QueryAsync<TMS_tyreDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TMS_tyreDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }
        #endregion

        #region 油费添加
        public async Task<int> TryAdds(TMS_tyreDto info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_tyreModel values ('{info.Id}','{info.TryNumber}','{info.Trypin}','{info.Trygui}','{info.Trysum}','{info.TryPeop}','{info.TryTim}','{info.TryTxt}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 油费删除
        public async Task<int> DeletTry(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_tyreModel where Id='{Id}'", null,
                 await GetDbTransactionAsync());

        }

        #endregion

        #region 油费修改
        public async Task<int> UpdateTryup(TMS_tyreDto tMS_Induction)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"update TMS_tyreModel set TryNumber='{tMS_Induction.TryNumber}',Trypin='{tMS_Induction.Trypin}',Trygui='{tMS_Induction.Trygui}',Trysum='{tMS_Induction.Trysum}',TryPeop='{tMS_Induction.TryPeop}',TryTim='{tMS_Induction.TryTim}',TryTxt='{tMS_Induction.TryTxt}' where Id='{tMS_Induction.Id}'", null, await GetDbTransactionAsync());
        }
        #endregion
    }
}
